<template>
  <div>
    <div>
      <el-form :inline="true"  :model="orderinfo" class="demo-form-inline">
        <el-form-item label="订单id" prop="id">
          <el-input v-model="orderinfo.id" readonly></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="name">
          <el-input v-for="pname in productnames" v-if="orderinfo.productid==pname.id" v-model="pname.productname" readonly></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            label="设备id"
            prop="equimentid"
            width="150">
        </el-table-column>

        <el-table-column
            label="设备名称"
            prop="euipmentname"
            width="120">
        </el-table-column>

        <el-table-column
            label="产品产能(件/小时)"
            prop="num">
        </el-table-column>

        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">排产
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <el-dialog :visible.sync="dialogVisible" title="订单排产" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="开始日期" required style="width: 570px">
          <el-col :span="11">
            <el-form-item prop="startdate">
              <el-date-picker v-model="form.startdate" placeholder="选择日期" style="width: 100%;" type="date"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>

        <el-form-item label="结束日期" required style="width: 570px">
          <el-col :span="11">
            <el-form-item prop="finshdate">
              <el-date-picker v-model="form.finishdate" placeholder="选择日期" style="width: 100%;" type="date"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="
        dialogVisible = false;
        this.$message({
          message: '排产取消!',
          type: 'warning'
      })"
        >取消</el-button>
        <el-button type="primary" @click="submitScheduling('form')">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      productnames:null,
      orderid:this.$route.query.orderid,
      tableData:null,
      dialogVisible:false,

      form:{
        startdate:null,
        finishdate:null,
        equipmentid:null,
        factoryid:this.$store.state.factoryid,
        orderid:this.$route.query.orderid,
      },
      orderinfo:{
        id:null,
        productid: null
      },

      //form表单内数据验证
      startdate: {
        disabledDate: time => {
          if (this.form.finishdate) {
            return (
                time.getTime() > new Date(this.form.finishdate).getTime()
            );
          } else {
            return time.getTime() > Date.now();
          }
        }
      },
      finishdate: {
        disabledDate: time => {
          if (this.form.startdate) {
            return (
                time.getTime() > Date.now() ||
                time.getTime() < new Date(this.form.startdate).getTime()
            );
          } else {
            return time.getTime() > Date.now();
          }
        }
      },
      rules:{
        startdate: [
          { required: true, message: '开始日期不能为空', trigger: 'blur' }
        ],
       finishdate: [
          { required: true, message: '结束日期不能为空', trigger: 'blur' }
        ],

      },
    };
  },created() {
    this.orderid=this.$route.query.orderid;
    this.getTableData();
    axios.get("http://localhost:8081/order/findById/"+this.orderid).then((response)=>{
      this.orderinfo = response.data;
    });
    axios.get('http://localhost:8081/order/getProductname').then(
        (response) => {
          this.productnames = response.data;
        }
    );

  },
  methods: {
    handleEdit(index, row) {
      this.form.equipmentid = row.equimentid;
      this.dialogVisible = true;
    },
    submitScheduling(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8081/scheduling/addscheduling',this.form).then((response)=>{
            if(response.data == "success"){
              this.$router.push('/managerequipmentlist');
              this.success();
            }else{
              this.failed();
            }
          })
        } else {
          this.failed();
          return false;
        }
      });
    },
    getTableData(){
      axios.get("http://localhost:8081/scheduling/getTableData/"+this.orderid).then((response)=>{
        this.tableData = response.data;
      });
    },
    success() {
      this.$message({
        message: '排产成功!',
        type: 'success'
      });
    },
    failed() {
      this.$message.error('修改失败，请检查内容是否正确或稍后再试!');
    }
  }
}
</script>